package com.zbystudy;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By zby on 22:39 2019/3/6
 * 排序
 */

public class Sort {

    /**
     * Created By zby on 14:55 2019/3/6
     * 获取升序排序后的值
     *
     * @param jsonObject json格式的数据
     * @param isAsc      true 升序  false 降序
     */
    public static List<String> keySort(JSONObject jsonObject, boolean isAsc) {
        if (null == jsonObject && jsonObject.size() <= 0) {
            return null;
        }
        List<String> keyList = new ArrayList<>(jsonObject.keySet());
        if (null != keyList && keyList.size() > 0) {
            for (int i = 0; i < keyList.size() - 1; i++) {
                for (int j = 0; j < keyList.size() - (i + 1); j++) {
                    KeysUtil test = new KeysUtil(keyList, j).invoke();
                    String currKey = test.getCurrKey();
                    String afterKey = test.getAfterKey();
                    char[] currKeyChars = test.getCurrKeyChars();
                    for (int k = 0; k < currKeyChars.length; k++) {
                        //保证当前字符是有效字符，即在26个字母之中,不在，直接放到后面
                        if (CharUtil.validateLetter(currKeyChars[k])) {
                            // 小于，不用排序，直接跳出
                            if (currKeyChars[k] < afterKey.charAt(k)) {
                                if (isAsc) {
                                    break;
                                } else {
                                    if (CharUtil.validateLetter(afterKey.charAt(k))) {
                                        keyList.set(j, afterKey);
                                        keyList.set(j + 1, currKey);
                                    }
                                }
                                //  等于，跳过此循环
                            } else if (currKeyChars[k] == afterKey.charAt(k)) {
                                continue;
                                //  大于，看清而定
                            } else {
                                if (isAsc) {
                                    if (CharUtil.validateLetter(afterKey.charAt(k))) {
                                        keyList.set(j, afterKey);
                                        keyList.set(j + 1, currKey);
                                    }
                                }
                                break;
                            }
                        } else {
                            if (isAsc) {
                                keyList.set(j, afterKey);
                                keyList.set(j + 1, currKey);
                            }
                            break;
                        }
                    }
                }
            }
        }
        return keyList;
    }

    /**
     * 使用嵌套类
     */
    private static class KeysUtil {

        /**
         * jsonObject的key值集合
         */
        private List<String> keyList;

        /**
         * 下标值
         */
        private int j;

        /**
         * 当前key值
         */
        private String currKey;

        /**
         * 下一个key值
         */
        private String afterKey;

        /**
         * 当前字符串key值的字符集
         */
        private char[] currKeyChars;

        public KeysUtil(List<String> keyList, int j) {
            this.keyList = keyList;
            this.j = j;
        }

        public String getCurrKey() {
            return currKey;
        }

        public String getAfterKey() {
            return afterKey;
        }

        public char[] getCurrKeyChars() {
            return currKeyChars;
        }

        public KeysUtil invoke() {
            currKey = keyList.get(j);
            afterKey = keyList.get(j + 1);
            if (StringUtils.isBlank(currKey) && StringUtils.isBlank(afterKey)) {
                throw new RuntimeException("当前值为空currKey=" + currKey + ",或者下一个值afterKey=" + afterKey);
            }
            currKeyChars = currKey.toCharArray();
            return this;
        }
    }
}
